<template>
	<view class="page">
		<view class="info_list">
			<view class="info_item" @tap="bindTo(0)">
				<text class="f_semibold f_32">基础信息</text>
				<view class="dis-flex flex-y-center">
					<text :class="['state_text', checkInfo[0] == 0 ? 'violet1' : '']">{{ checkInfo[0] == 0 ? '已' : '未' }}填写</text>
					<image class="icon" :src="checkInfo[0] == 0?imgPrefix('/static/image/icon/address_next.png'):imgPrefix('/static/image/teacher/next_icon_gery.png')"></image>
				</view>
			</view>
			<view class="info_item" @tap="bindTo(1)">
				<text class="f_semibold f_32">课程价格</text>
				<view class="dis-flex flex-y-center">
					<text :class="['state_text', checkInfo[1] == 0 ? 'violet1' : '']">{{ checkInfo[1] == 0 ? '已' : '未' }}填写</text>
					<image class="icon" :src="checkInfo[1] == 0?imgPrefix('/static/image/icon/address_next.png'):imgPrefix('/static/image/teacher/next_icon_gery.png')"></image>
				</view>
			</view>
			<view class="info_item" @tap="bindTo(2)">
				<text class="f_semibold f_32">授课时间</text>
				<view class="dis-flex flex-y-center">
					<text :class="['state_text', checkInfo[2] == 0 ? 'violet1' : '']">{{ checkInfo[2] == 0 ? '已' : '未' }}填写</text>
					<image class="icon" :src="checkInfo[2] == 0?imgPrefix('/static/image/icon/address_next.png'):imgPrefix('/static/image/teacher/next_icon_gery.png')"></image>
				</view>
			</view>
			<view class="info_item" @tap="bindTo(3)">
				<text class="f_semibold f_32">
					教学经历
					<text class="f_26 gery1">(选填)</text>
				</text>
				<view class="dis-flex flex-y-center">
					<text :class="['state_text', checkInfo[3] == 0 ? 'violet1' : '']">{{ checkInfo[3] == 0 ? '已' : '未' }}填写</text>
					<image class="icon" :src="checkInfo[3] == 0?imgPrefix('/static/image/icon/address_next.png'):imgPrefix('/static/image/teacher/next_icon_gery.png')"></image>
				</view>
			</view>
		</view>
		<fixed-bottom>
			<view :class="['fixed_btn',checkInfo[0]==0&&checkInfo[1]==0&&checkInfo[2]==0?'active_btn':'']" @tap="postFrom">确认</view>
		</fixed-bottom>
	</view>
</template>

<script>
import { commonMixinFun } from '@/static/mixins/common.js';
import API from '@/static/api/index.js';
const app = getApp();
export default {
	mixins: [commonMixinFun],
	data() {
		return {
			infoObj: {
				basics: {},
				coursePrice: {},
				time: [],
				stype: {}
			},
			sendType:0 ,// 0新增  1修改
			teacherInfo:{},
			dayStrArr:['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
		};
	},
	
	onLoad(option) {
		// let pages = getCurrentPages();  // 获取当前页面栈
		// let prevPage = pages[pages.length - 2]; // -2 就是你上一页的数据 你上上页的数据就是-3 了以此类推！
		// app._log("prevPage.route",prevPage.route)
		// if(prevPage.route=="pages/index/index"){ // 首页或者我的页面跳转过来的是去修改的
			API.getTeacherInfo().then(({code,data,msg}) => {
				if (code == 1) {
					app._log(data.teacher)
					if(data.teacher.teacher_course!=null&&data.teacher.teacher_course!=''){
						this.sendType=1
						this.teacherInfo = data.teacher;
						this.$forceUpdate();
					}
				}else{
					this.showToast(msg||'获取教师信息失败，请稍后再试')
				}
			}).catch(err=>{
				this.showToast(err.msg||'获取教师信息失败，请稍后再试')
			});
		// }
	},
	watch:{
		teacherInfo(val,oldVal){ // 获取到教师信息后
			let {teacher_course,busy,trait,tag,experience}=val
			let status = val.teacher_course!=null
			let obj = {
				basics:{//基础信息
					address:val.address,
					offline:status?teacher_course.offline:1,
					language_id:status?teacher_course.language_id:'',
					language_grade_id:status?teacher_course.language_grade_id:'',
					latitude:val.latitude,
					longitude:val.longitude,
					stage:status&&teacher_course.stage!=''?teacher_course.stage.split(','):[],//授课阶段
					classdata:status&&teacher_course.classdata,//授课方式
					speaking:status&&teacher_course.speaking!=''?teacher_course.speaking.split(','):[]//授课语言
				},
				coursePrice:{ //价格信息
					hour:status?teacher_course.hour:'', // 课时
					price:status?teacher_course.price:''//价格
				}
			}
			
			// 授课时间信息
			let timeArr = [];
			if(busy!=null){
				let {dayStrArr}=this;
				let timeList = ['06:00-12:00', '12:00-18:00', '18:00-24:00'];
				dayStrArr.forEach(e=>{
					// app._log(e,busy[e])
					let arr = [];
					timeList.forEach(q=>{
						arr.push(busy[e]!=''?busy[e].indexOf(q)==-1:true)
					})
					timeArr.push(arr)
				})
			}
			obj.time = timeArr
			
			
			// 教学经历
			let stype = {
				trait:trait,
				experience:experience
			}
			let tagIdArr = []
			tag.forEach(e=>{
				tagIdArr.push(e.tag_id)
			})
			stype.tag=tagIdArr;
			obj.stype = stype
			
			this.infoObj=obj;
			this.$forceUpdate()
		}
	},
	computed: {
		checkInfo: function() {
			let { basics, coursePrice, time, stype } = this.infoObj;
			return [
				JSON.stringify(basics) == '{}' ? '请填写基础信息' : 0,
				JSON.stringify(coursePrice) == '{}' ? '请填写课程价格' : 0,
				JSON.stringify(time) == '[]' ? '请填写授课时间' : 0,
				JSON.stringify(stype) == '{}' ? '请填写教学经历' : 0
			];
		},
		sendFrom(){ // 提交的数据
			let { basics, coursePrice, time, stype } = this.infoObj;
			// app._log(basics,coursePrice)
			let tempObj = {};
			let { teacher_course } = this.teacherInfo;
			if(teacher_course!=null&&teacher_course!=''){
				tempObj.teacher_course_id=teacher_course.teacher_course_id;
			}
			let newObj = {}
			if(Array.isArray(basics.speaking)){
				basics.speaking = basics.speaking.join(',')
			}
			if(Array.isArray(basics.stage)){
				basics.stage = basics.stage.join(',')
			}
			let timeArr = ['06:00-12:00', '12:00-18:00', '18:00-24:00'];
			let {dayStrArr}=this
			let busy_time = {};
			if(time!=undefined&&time.length>0){
				time.forEach((e,i)=>{
					let arr = [];
					e.forEach((eq,ei)=>{
						if(!eq){
							arr.push(timeArr[ei])
						}
					})
					busy_time[dayStrArr[[i]]] = arr.join(',')
				})
			}
			tempObj.busy_time = busy_time
				
			newObj = Object.assign(tempObj,basics,coursePrice,stype)
			return newObj;
		}
	},
	onShow(){
		uni.$on("setObj",({type,objList})=>{
			app._log(type,objList);
			if(type==1){
				this.infoObj.basics = objList
			}else if(type==2){
				this.infoObj.coursePrice = objList
			}else if(type==3){
				this.infoObj.time = objList
			}else if(type==4){
				this.infoObj.stype = objList
			}
			uni.$off('setObj');
		});
	},
	methods:{
		bindTo(index){
			app._log(index)
			let { basics, coursePrice, time, stype } = this.infoObj;
			switch(Number(index)){
				case 0://基础信息
				this.pageTo('/pages/teacher/basic_information/index',{basics:JSON.stringify(basics)})
				break;
				case 1://课程价格
				this.pageTo('/pages/teacher/price_of_course/index',{coursePrice:JSON.stringify(coursePrice)})
				break;
				case 2://授课时间
				this.pageTo('/pages/teacher/select_time/index',{time:JSON.stringify(time)})
				break;
				case 3://教学经历
				this.pageTo('/pages/teacher/teaching_experience/index',{stype:JSON.stringify(stype)})
				break;
			}
		},
		postFrom(){
			app._log(523)
			let {checkInfo,sendType,sendFrom}=this;
			let index = checkInfo.findIndex(o=>o!=0);
			if(index>-1&&index<3){
				this.showToast(checkInfo[index])
			}else{
				// app._log(JSON.stringify((sendFrom)))
				let send = sendType==0?API.addCourseInfo(sendFrom):API.editCourseInfo(sendFrom);
				send.then(({code,msg,data})=>{
					app._log(code,msg,data)
					if(code==1){
					app._log('你进来？')
						if(sendType==0){
							this.showToast('填写成功，正在跳转')
						}else{
							this.showToast('修改成功，正在跳转')
						}
						setTimeout(()=>{
							if(sendType==0){
								this.pageTo('/pages/index/index')
							}else{
								uni.navigateBack()
							}
						},1500)
					}else{
						app._log('你怎么进来；了',code,code==1,typeof code)
						this.showToast(msg||((sendType==0?'填写':'修改')+'失败，请稍后再试'))
					}
				}).catch(err=>{
					app._log('你失败了？')
					this.showToast(err.msg||((sendType==0?'填写':'修改')+'失败，请稍后再试'))
				})
			}
		}
	}
};
</script>

<style lang="scss">
.info_list {
	padding: 0 48rpx;
	background-color: #fff;
	.info_item {
		height: 128rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		&:not(:last-child) {
			border-bottom: 2rpx solid #f8f8f8;
		}
		.state_text {
			height: 40rpx;
			font-size: 28rpx;
			&:not(.violet1){
				color: #8f8f9a;
			}
		}
		.icon {
			padding-left: 8rpx;
			width: 32rpx;
			height: 32rpx;
		}
	}
}
.fixed_btn {
	margin-left: 48rpx;
	width: 654rpx;
	height: 92rpx;
	display: flex;
	border-radius: 16rpx;
	font-size: 32rpx;
	align-items: center;
	justify-content: center;
	&.active_btn {
		color: #fff;
		background-image: linear-gradient(270deg, #7f4aff 0%, #9254ff 100%);
	}
	&:not(.active_btn) {
		color: #d1d2db;
		background-color: #f0f0f6;
	}
}
</style>
